package top.kaoshanji.p2ch06a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.*;

/**
 * 代码清单 p2ch06a-01：用try-with-resources语句进行数据库操作的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/17 15:33
 */
public class TryWithResourcesExample01 {

    private static final Logger logger = LogManager.getLogger(TryWithResourcesExample01.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p2ch06a-01：用try-with-resources语句进行数据库操作的示例...");

        try (Connection connection = DriverManager.getConnection("jdbc:derby://localhost/p2-java7book");
             Statement stmt = connection.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM book")){
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

}
